package com.pbd.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.pbd.shared.ALBUMMASTER;
import com.pbd.shared.BUSINESSCATEGORY;
import com.pbd.shared.BUSINESSCATEGORYRELATION;
import com.pbd.shared.CORRESPONDENCEDETAIL;
import com.pbd.shared.MEDIARELATION;
import com.pbd.shared.PARTY;
import com.pbd.shared.PARTYCONTACT;

public class MemberRegistrationScreen extends Composite {

	private TextBox nameBox;
	private RadioButton personRadio;
	private RadioButton orgRadio;
	private PbdListBox bcListBox;
	private DateBox joinedDateBox;
	private TextArea addressTextArea;
	private SuggestBox areaBox;
	private SuggestBox cityBox;
	// private SuggestBox stateBox;
	private TextBox pinCode;
	private TextBox contactPerson;
	private TextBox webSite;
	private Button saveButton;
	private boolean isValid;
	private FlexTable correDetailsTable;
	private TextArea textAreaKeyWords;
	private MultiWordSuggestOracle areaMWSOracle;
	private Long partyId;
	private PARTY party;
	private PBDFileUpload pbdFileUpload;
	private List logoList;
	private DeckPanel deckPanel;

	// private Label errorLabel;

	public MemberRegistrationScreen() {
		super();
		init();

	}

	public MemberRegistrationScreen(Long partyId) {
		super();
		this.partyId = partyId;
		init();

	}

	private void init() {
		deckPanel = new DeckPanel();
		VerticalPanel mainPanel = new VerticalPanel();
		VerticalPanel fpPanel = new VerticalPanel();
		fpPanel.setStyleName("left_blue_box");
		fpPanel.setWidth("660px");

		VerticalPanel spPanel = new VerticalPanel();
		spPanel.setStyleName("left_blue_box");
		spPanel.setWidth("660px");
		FlexTable basicInfoTable = new FlexTable();
		// basicInfoTable.setStyleName("left_blue_box");

		saveButton = new Button("Submit");
		addClickHandler();
		DisclosurePanel basicInfoDCP = new DisclosurePanel("Basic Information", true);

		basicInfoDCP.add(basicInfoTable);
		addBasicInfoTable(basicInfoTable);

		DisclosurePanel contactInfoDCP = new DisclosurePanel("Contact Information", true);

		correDetailsTable = new FlexTable();
		// correDetailsTable.setStyleName("left_blue_box");
		VerticalPanel vpCorresDetail = new VerticalPanel();
		contactInfoDCP.add(vpCorresDetail);
		vpCorresDetail.add(correDetailsTable);
		Button addButton = new Button("Add");
		Button removeButton = new Button("Remove");
		HorizontalPanel hpButton = new HorizontalPanel();
		hpButton.add(addButton);
		hpButton.add(removeButton);
		addButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				addContactInfoTable(correDetailsTable, null, null, null);

			}
		});
		removeButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				removeRow();

			}
		});
		vpCorresDetail.add(hpButton);
		if (partyId == null)
			addContactInfoTable(correDetailsTable, null, null, null);

		DisclosurePanel keywordDCP = new DisclosurePanel("Keywords", true);

		fpPanel.add(basicInfoDCP);
		fpPanel.add(contactInfoDCP);
		fpPanel.add(keywordDCP);
		fpPanel.setCellHorizontalAlignment(basicInfoDCP, HasHorizontalAlignment.ALIGN_CENTER);
		fpPanel.setCellHorizontalAlignment(contactInfoDCP, HasHorizontalAlignment.ALIGN_CENTER);
		fpPanel.setCellHorizontalAlignment(keywordDCP, HasHorizontalAlignment.ALIGN_CENTER);
		textAreaKeyWords = new TextArea();
		textAreaKeyWords.setWidth("400px");
		keywordDCP.add(textAreaKeyWords);
		fpPanel.add(saveButton);
		fpPanel.setCellHorizontalAlignment(saveButton, HasHorizontalAlignment.ALIGN_CENTER);
		HTML htmlAlbums = new HTML("Go to Albums");
		htmlAlbums.setStyleName("heading_blue");
		fpPanel.add(htmlAlbums);
		fpPanel.setCellHorizontalAlignment(htmlAlbums, HasHorizontalAlignment.ALIGN_RIGHT);
		htmlAlbums.setVisible(false);
		mainPanel.add(deckPanel);
		initWidget(mainPanel);
		deckPanel.add(fpPanel);

		if (partyId != null) {
			htmlAlbums.setVisible(true);
			htmlAlbums.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					deckPanel.showWidget(1);

				}
			});

			spPanel.add(new AlbumCreation(partyId));
			HTML htmlProfile = new HTML("Back to Profile");
			htmlProfile.setStyleName("heading_blue");
			spPanel.add(htmlProfile);
			spPanel.setCellHorizontalAlignment(htmlProfile, HasHorizontalAlignment.ALIGN_LEFT);
			htmlProfile.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					deckPanel.showWidget(0);

				}
			});

			deckPanel.add(spPanel);
			ServiceUtil.getMemberServiceAsync().getParty(partyId, new AsyncCallback<Map>() {

				@Override
				public void onFailure(Throwable caught) {
					caught.printStackTrace();

				}

				@Override
				public void onSuccess(Map result) {
					System.err.println("result" + result.toString());
					List partyInfoList = (List) result.get("PARTYINFO");
					List bcList = (List) result.get("BUSINESSCATEGORY");
					List pcList = (List) result.get("PARTYCONTACT");
					List corrList = (List) result.get("CORRESPONDENCEDETAILS");
					List logoAlbumList = (List) result.get("LOGOALBUM");
					ArrayList partyDataValueList = (ArrayList) partyInfoList.get(0);
					party = new PARTY();
					party.setPartyId(Long.valueOf((Integer) partyDataValueList.get(0)));
					nameBox.setValue((String) partyDataValueList.get(1));
					if (((String) partyDataValueList.get(2)).equalsIgnoreCase("Org"))
						orgRadio.setValue(true);
					else
						personRadio.setValue(true);
					webSite.setValue((String) partyDataValueList.get(3));
					contactPerson.setValue((String) partyDataValueList.get(4));
					textAreaKeyWords.setValue((String) partyDataValueList.get(5));
					joinedDateBox.setValue((java.util.Date) partyDataValueList.get(6));
					party.setIsActive((java.lang.Boolean) partyDataValueList.get(7));
					for (int index = 0; index < bcList.size(); index++) {
						ArrayList bcArrayList = (ArrayList) bcList.get(index);
						if (((Boolean) bcArrayList.get(3))) {
							System.err.println("------------------" + (String) bcArrayList.get(2) + "--------------");
							bcListBox.setLayoutData(Long.valueOf((Integer) bcArrayList.get(0)));
							populateBusinessCategory(bcListBox, (String) bcArrayList.get(2));
						}
					}

					for (int index = 0; index < pcList.size(); index++) {
						ArrayList pcArrayList = (ArrayList) pcList.get(index);
						if (((String) pcArrayList.get(7)).equalsIgnoreCase("Permanent")) {
							addressTextArea.setLayoutData(Long.valueOf((Integer) pcArrayList.get(0)));
							addressTextArea.setValue((String) pcArrayList.get(1));
							cityBox.setValue((String) pcArrayList.get(3));
							populateArea((String) pcArrayList.get(3), areaBox, (String) pcArrayList.get(2));
							pinCode.setValue((String) pcArrayList.get(6));
						}

					}

					for (int index = 0; index < corrList.size(); index++) {
						ArrayList corresArrayList = (ArrayList) corrList.get(index);
						addContactInfoTable(correDetailsTable, Long.valueOf((Integer) corresArrayList.get(0)), (String) corresArrayList.get(1), (String) corresArrayList.get(2));
					}

					if (logoAlbumList != null && logoAlbumList.size() > 0)
						logoList = (ArrayList) logoAlbumList.get(0);
				}
			});
		}
		deckPanel.showWidget(0);
	}

	private void addContactInfoTable(FlexTable contactInfoTable, Long correspondenceId, String value, String type) {
		PbdListBox pbdListBox = new PbdListBox("200px");
		List<KeyValuePair> keyValueList = new ArrayList<KeyValuePair>();
		keyValueList.add(new KeyValuePair(-1L, "---Select---"));
		keyValueList.add(new KeyValuePair(1L, "Email"));
		keyValueList.add(new KeyValuePair(2L, "Fax"));
		keyValueList.add(new KeyValuePair(3L, "Mobile"));
		keyValueList.add(new KeyValuePair(4L, "Phone"));
		pbdListBox.setValueList(keyValueList);

		int rowCount = contactInfoTable.getRowCount();
		contactInfoTable.setWidget(rowCount, 0, new CheckBox());
		contactInfoTable.setWidget(rowCount, 1, pbdListBox);
		if (type != null)
			pbdListBox.setValue(type);
		TextBox valueTextBox = new TextBox();
		if (value != null)
			valueTextBox.setValue(value);
		contactInfoTable.setWidget(rowCount, 2, valueTextBox);
		if (correspondenceId != null) {
			Hidden hidden = new Hidden();
			hidden.setLayoutData(correspondenceId);
			contactInfoTable.setWidget(rowCount, 3, hidden);
		}
	}

	private void removeRow() {
		for (int index = 0; index < correDetailsTable.getRowCount(); index++) {
			CheckBox checkbox = (CheckBox) correDetailsTable.getWidget(index, 0);
			if (checkbox.getValue())
				correDetailsTable.removeRow(index);

		}
	}

	private void addBasicInfoTable(FlexTable basicInfoTable) {
		nameBox = new TextBox();
		nameBox.setWidth("200px");
		basicInfoTable.setWidget(0, 0, new Label("Member Name"));
		basicInfoTable.setWidget(0, 1, nameBox);

		personRadio = new RadioButton("membertype");
		personRadio.setText("Person");
		orgRadio = new RadioButton("membertype");
		orgRadio.setText("Organization");
		basicInfoTable.setWidget(1, 0, new Label("Member Type"));

		HorizontalPanel radioPanel = new HorizontalPanel();
		radioPanel.setSpacing(2);
		radioPanel.add(personRadio);
		radioPanel.add(orgRadio);
		orgRadio.setValue(true);
		basicInfoTable.setWidget(1, 1, radioPanel);

		bcListBox = new PbdListBox("200px");
		basicInfoTable.setWidget(2, 0, new Label("Business category"));
		basicInfoTable.setWidget(2, 1, bcListBox);

		joinedDateBox = new DateBox();
		joinedDateBox.setWidth("200px");
		basicInfoTable.setWidget(3, 0, new Label("Joined Date"));
		basicInfoTable.setWidget(3, 1, joinedDateBox);

		joinedDateBox.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("MMM d, y")));

		addressTextArea = new TextArea();
		addressTextArea.setWidth("200px");
		basicInfoTable.setWidget(4, 0, new Label("Address"));
		basicInfoTable.setWidget(4, 1, addressTextArea);

		MultiWordSuggestOracle cityMWSOracle = new MultiWordSuggestOracle();
		cityMWSOracle.add("Chennai");
		cityMWSOracle.add("Coimbatore");
		cityMWSOracle.add("Madurai");
		cityMWSOracle.add("Salem");
		cityMWSOracle.add("Trichy");
		cityMWSOracle.add("Tirunelveli");

		areaMWSOracle = new MultiWordSuggestOracle();
		areaBox = new SuggestBox(areaMWSOracle);
		areaBox.setWidth("200px");
		cityBox = new SuggestBox(cityMWSOracle);

		cityBox.addSelectionHandler(new SelectionHandler() {
			@Override
			public void onSelection(SelectionEvent event) {
				System.err.println(cityBox.getValue());
				populateArea(cityBox.getValue(), null, null);
				areaBox.setValue("");

			}
		});

		cityBox.setWidth("200px");
		basicInfoTable.setWidget(5, 0, new Label("City"));
		basicInfoTable.setWidget(5, 1, cityBox);

		basicInfoTable.setWidget(6, 0, new Label("Area"));
		basicInfoTable.setWidget(6, 1, areaBox);

		pinCode = new TextBox();
		pinCode.setWidth("200px");
		pinCode.setMaxLength(6);
		basicInfoTable.setWidget(7, 0, new Label("Pin Code"));
		basicInfoTable.setWidget(7, 1, pinCode);
		addNumberFieldValidation(pinCode);

		contactPerson = new TextBox();
		contactPerson.setWidth("200px");
		basicInfoTable.setWidget(8, 0, new Label("Contact Person"));
		basicInfoTable.setWidget(8, 1, contactPerson);

		webSite = new TextBox();
		webSite.setWidth("200px");
		basicInfoTable.setWidget(9, 0, new Label("Web Site"));
		basicInfoTable.setWidget(9, 1, webSite);

		pbdFileUpload = new PBDFileUpload(partyId, "Logo", new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(String result) {
				if (logoList == null)
					logoList = new ArrayList();
				if (logoList.size() > 1)
					logoList.set(0, result);
				else
					logoList.add(result);
				System.err.println("result" + result + "logoList.size" + logoList.size());
			}
		});
		basicInfoTable.setWidget(10, 0, new Label("Logo"));
		basicInfoTable.setWidget(10, 1, pbdFileUpload);

		basicInfoTable.getFlexCellFormatter().setWidth(0, 0, "200px");
		basicInfoTable.getFlexCellFormatter().setVerticalAlignment(10, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		if (partyId == null)
			populateBusinessCategory(bcListBox, null);

	}

	private void addNumberFieldValidation(final TextBox textBox) {
		textBox.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				TextBox sender = (TextBox) event.getSource();

				if (sender.isReadOnly() || !sender.isEnabled()) {
					return;
				}

				Character charCode = event.getCharCode();

				int unicodeCharCode = event.getUnicodeCharCode();

				// allow digits, '.' and non-characters
				if (!(Character.isDigit(charCode) || charCode == '.' || unicodeCharCode == 0)) {
					sender.cancelKey();
				}

			}
		});

	}

	private void populateArea(final String cityName, final SuggestBox suggestBox, final String areaName) {
		areaMWSOracle.clear();
		ServiceUtil.getHibernateServiceAsync().findObjects("AREA", new String[] { "areaName" }, "cityName='" + cityName + "'", "areaName", false, true, new AsyncCallback() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(Object result) {
				List dataList = (List) result;
				areaMWSOracle.addAll(dataList);

				if (suggestBox != null)
					suggestBox.setValue(areaName);
			}
		});
	}

	private void addClickHandler() {
		saveButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				isValid = true;
				if (party == null)
					party = new PARTY();
				if (nameBox.getText().trim() != null && nameBox.getText().trim().length() > 0) {
					party.setName(nameBox.getText().trim());
				} else
					showAlert(nameBox, "Member Name is mandatory !");
				System.err.println("isValid" + isValid);

				if (personRadio.getValue())
					party.setType("Person");
				else
					party.setType("Org");

				party.setCreatedDate(new Date());

				if (party.getPartyId() == null)
					party.setIsActive(true);
				

				if (textAreaKeyWords.getText() != null && textAreaKeyWords.getText().length() > 0)
					party.setKeywords(textAreaKeyWords.getText());

				if (contactPerson.getText() != null && contactPerson.getText().length() > 0)
					party.setContactPerson(contactPerson.getText());
				if (webSite.getText() != null && webSite.getText().length() > 0)
					party.setWebSite(webSite.getText());

				Map<String, Set> partyRelatedInfoMap = new HashMap<String, Set>();
				Set<PARTYCONTACT> partyContactSet = new HashSet<PARTYCONTACT>();
				PARTYCONTACT partyContact = new PARTYCONTACT();

				if (addressTextArea.getLayoutData() != null)
					partyContact.setPartyContactId((Long) addressTextArea.getLayoutData());

				if (addressTextArea.getValue() != null && addressTextArea.getValue().length() > 0)
					partyContact.setAddress(addressTextArea.getValue());
				else
					showAlert(addressTextArea, "Address is mandatory !");

				if (cityBox.getValue() != null && cityBox.getValue().length() > 0)
					partyContact.setCityName(cityBox.getValue());
				else
					showAlert(cityBox, "Choose the city");

				if (areaBox.getValue() != null && areaBox.getValue().length() > 0)
					partyContact.setArea(areaBox.getValue());
				else
					showAlert(areaBox, "Choose the city area");

				partyContact.setCountryName("INDIA");
				partyContact.setType("Permanent");
				partyContact.setStateName("Tamil nadu");
				partyContact.setPinCode(pinCode.getValue());

				partyContact.setParty(party);

				partyContactSet.add(partyContact);

				partyRelatedInfoMap.put("partyContact", partyContactSet);

				if (bcListBox != null && bcListBox.getValue() != null && bcListBox.getValue().length() > 0) {
					if (bcListBox.getKey(bcListBox.getValue()) != -1) {
						System.err.println("bcListBox.getKey(bcListBox.getValue()" + bcListBox.getKey(bcListBox.getValue()));
						Set<BUSINESSCATEGORYRELATION> bcrSet = new HashSet<BUSINESSCATEGORYRELATION>();
						BUSINESSCATEGORYRELATION businessCategoryRelation = new BUSINESSCATEGORYRELATION();
						if (bcListBox.getLayoutData() != null)
							businessCategoryRelation.setId((Long) bcListBox.getLayoutData());
						BUSINESSCATEGORY businessCategory = new BUSINESSCATEGORY();
						businessCategory.setId(bcListBox.getKey(bcListBox.getValue()));
						businessCategory.setName(bcListBox.getValue());

						businessCategoryRelation.setBusinessCategory(businessCategory);
						businessCategoryRelation.setIsPrimary(true);
						bcrSet.add(businessCategoryRelation);
						partyRelatedInfoMap.put("businessCategory", bcrSet);
					} else
						showAlert(bcListBox, "Business category is mandatory !");

				}

				if (correDetailsTable.getRowCount() == 0)
					showAlert(correDetailsTable, "Atleat one correspondence details is required !");
				else {
					int rowCount = correDetailsTable.getRowCount();
					Set<CORRESPONDENCEDETAIL> correspondenceSet = new HashSet<CORRESPONDENCEDETAIL>();
					for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
						PbdListBox corresType = (PbdListBox) correDetailsTable.getWidget(rowIndex, 1);
						TextBox contactValue = (TextBox) correDetailsTable.getWidget(rowIndex, 2);

						if (corresType != null && !corresType.getValue().equalsIgnoreCase("---Select---")) {
							CORRESPONDENCEDETAIL correspondenceDetail = new CORRESPONDENCEDETAIL();
							if (correDetailsTable.getCellCount(rowIndex) > 3) {
								correspondenceDetail.setCorrespondenceDetailId((Long) ((Hidden) correDetailsTable.getWidget(rowIndex, 3)).getLayoutData());
								correspondenceDetail.setUpdatedDate(new Date());
							}
							correspondenceDetail.setType(corresType.getValue());

							if (contactValue.getText() != null && contactValue.getText().length() > 0) {
								correspondenceDetail.setValue(contactValue.getText());
								correspondenceSet.add(correspondenceDetail);
							} else {
								showAlert(correDetailsTable, "Enter the " + corresType.getValue() + " value");
								break;
							}

						}
					}

					if (correspondenceSet.size() > 0)
						partyRelatedInfoMap.put("correspondenceDetails", correspondenceSet);
					else
						showAlert(correDetailsTable, "Atleat one correspondence detail is required !");

					if (logoList != null && logoList.size() > 0) {
						ALBUMMASTER albumMaster = new ALBUMMASTER();

						albumMaster.setAlbumTitle("Logo");
						albumMaster.setLayout("Single Photo");
						MEDIARELATION mediaRelation = new MEDIARELATION();
						mediaRelation.setIsDefault(true);
						mediaRelation.setMediaType("Logo/Image");
						mediaRelation.setMediaSource((String) logoList.get(0));
						if (logoList.size() > 1) {
							mediaRelation.setId((Long) logoList.get(1));
							albumMaster.setAlbumId((Long) logoList.get(2));
						} else
							albumMaster.setCreatedDate(new java.util.Date());

						Set albumMediaRelation = new HashSet();
						albumMediaRelation.add(mediaRelation);
						albumMaster.setAlbumMediaRelation(albumMediaRelation);

						Set albumSet = new HashSet();
						albumSet.add(albumMaster);
						partyRelatedInfoMap.put("logoAlbum", albumSet);
					}
				}

				party.setJoinedDate(joinedDateBox.getValue());
				if (isValid) {
					ServiceUtil.getMemberServiceAsync().saveOrUpdateMember(party, partyRelatedInfoMap, new AsyncCallback() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onSuccess(Object result) {
							if (((String) result).equalsIgnoreCase("Success"))
								Window.alert("Account saved successfully for " + party.getName());
							reset();
						}
					});
				}
			}
		});

	}

	private void reset() {
		nameBox.setValue("");
		orgRadio.setValue(true);
		bcListBox.setValue("---Select---");
		joinedDateBox.getElement().setAttribute("value", "");
		addressTextArea.setValue("");
		areaBox.setValue("");
		cityBox.setValue("");
		// private SuggestBox stateBox;
		pinCode.setValue("");
		contactPerson.setValue("");
		webSite.setValue("");
		isValid = false;
		correDetailsTable.clear();
		addContactInfoTable(correDetailsTable, null, null, null);
		textAreaKeyWords.setValue("");
		logoList = null;

	}

	private void showAlert(Widget widget, String message) {
		if (isValid) {
			isValid = false;
			int absoluteLeft = widget.getAbsoluteLeft();
			int absoluteTop = widget.getAbsoluteTop();
			PopupPanel popupPanel = new PopupPanel();
			popupPanel.setAutoHideEnabled(true);
			popupPanel.setPopupPosition(absoluteLeft + 250, absoluteTop);
			popupPanel.add(new HTML("<h7><font color=\"navy\">" + message + "</font></h7>"));
			popupPanel.setHeight("8px");
			popupPanel.show();

		}

	}

	private void populateBusinessCategory(final PbdListBox pbdListBox, final String value) {
		ServiceUtil.getHibernateServiceAsync().findObjects("BUSINESSCATEGORY", new String[] { "id", "name" }, "isActive=true", "name", false, true, new AsyncCallback() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(Object result) {
				List dataList = (List) result;
				Iterator iteator = dataList.iterator();
				List keyValueList = new ArrayList();
				KeyValuePair keyValue = new KeyValuePair();
				keyValue.setKey(-1L);
				keyValue.setValue("---Select---");

				keyValueList.add(keyValue);
				while (iteator.hasNext()) {
					ArrayList dataValueList = (ArrayList) iteator.next();
					keyValue = new KeyValuePair();
					keyValue.setKey((Long) dataValueList.get(0));
					keyValue.setValue((String) dataValueList.get(1));
					keyValueList.add(keyValue);
				}
				pbdListBox.setValueList(keyValueList);
				if (value != null) {
					System.err.println("AAAAAAAAAAAAAAAAAAAAAA");
					pbdListBox.setValue(value);
				} else
					pbdListBox.setValue("---Select---");
			}
		});
	}

	public class PbdFocusHandler implements FocusHandler {

		private Label errorLabel;

		public PbdFocusHandler(Label label) {
			super();
			this.errorLabel = label;
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onFocus(FocusEvent event) {
			errorLabel.setText("");

		}

	}

}
